/** index.js By Beginner Emain:zheng_jinfan@126.com HomePage:http://www.zhengjinfan.cn */

var tab;

layui.config({
    base: '../../script/private/'
}).use(['element', 'layer', 'navbar', 'tab', 'form'], function() {
    var element = layui.element(),
        $ = layui.jquery,
        layer = layui.layer,
        form = layui.form(),
        navbar = layui.navbar(),
        $ajax = layui.$ajax;

    window.eleOuter = element;
    window.$$ = $;
    window.$layer = layui.$layer;
    window.$navbar = navbar;

    tab = layui.tab({
        elem: '.admin-nav-card', //设置选项卡容器
        maxSetting: {
            max: 5,
            tipMsg: '只能开5个哇，不能再开了。真的。'
        },
        contextMenu: true
    });

    window.autoIframe = function(obj) {
        $(obj).on('resize', function() {
            var $that = $(this);
            var sideWidth = $('#admin-side').outerWidth();
            var $content = $('.admin-nav-card .layui-tab-content');
            $content.height($that.height() - 123);
            $content.width($that.width() - sideWidth);
            $content.find('iframe').each(function() {
                $(this).height($content.height());
                $(this).width($content.width());
            });
        }).resize();

    }



    $ajax.ajaxResponse({
        surl: '../../json/nav.json',
        dAsync: 'true',
        sFn: function(data) {
            //设置navbar
            navbar.set({
                spreadOne: true,
                elem: '#admin-navbar-side',
                cached: false,
                data: data
                    //url:''
            });

            //渲染navbar
            navbar.render();

            //监听点击事件
            navbar.on('click(side)', function(data) {
                tab.tabAdd(data.field);
                navbar.config.elem.closest('body').find('.layui-tab-title li').first().find('i:eq(1)').hide(); //2017.3.22 updata close
            });

            //默认显示首页并选中
            //navbar.config.elem.find('li:eq(0)').click().removeClass('layui-nav-itemed');
        }
    });

    //缩进左边
    $('.admin-side-toggle').on('click', function() {
        var sideWidth = $('#admin-side').width();
        var $content = $('.admin-nav-card .layui-tab-content');
        if (sideWidth === 200) {
            $('#admin-body').animate({
                left: '0'
            }); //admin-footer
            $('#admin-footer').animate({
                left: '0'
            });
            $('#admin-side').animate({
                width: '0'
            });

            $content.width($(window).width());
            $content.find('iframe').each(function() {
                $(this).width($content.width());

            });
        } else {
            $('#admin-body').animate({
                left: '200px'
            });
            $('#admin-footer').animate({
                left: '200px'
            });
            $('#admin-side').animate({
                width: '200px'
            });

            $content.width($(window).width() - 200);
            $content.find('iframe').each(function() {
                $(this).width($content.width());

            });
        }

    });

    //全屏单击
    $('.admin-side-full').on('click', fullScreen);

    //锁屏
    $(document).on('keydown', function() {
        var e = window.event;
        if (e.keyCode === 76 && e.altKey) {
            lock($, layer);
        }
    });
    $('#lock').on('click', function() {
        lock($, layer);
    });


    //手机设备的简单适配
    var treeMobile = $('.site-tree-mobile'),
        shadeMobile = $('.site-mobile-shade');
    treeMobile.on('click', function() {
        $('body').addClass('site-mobile');
    });
    shadeMobile.on('click', function() {
        $('body').removeClass('site-mobile');
    });

    //tab点击切换navbar相应的选中
    $(document).on('click', '.layui-tab-title li', function(data) {
        var $this = $(this);
        var str = $this.find('cite').attr('sid');
        if (typeof str == 'undefined') {
            return;
        } else {
            $('[lay-filter="' + str + '"]').click();
            if ($('[lay-filter="' + str + '"]').closest('dl').length > 0) {
                $('[lay-filter="' + str + '"]').closest('li').addClass('layui-nav-itemed');
                $('[lay-filter="' + str + '"]').click();
            }
        }
    });

    //禁用鼠标右键
    $(document).on('contextmenu keydown', '.layui-layer-shade', function(ev) {
        if (ev.type == 'keydown') {
            var keyCode = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
            if (keyCode == 116) {
                return false;
            }
        } else if (ev.type == 'contextmenu') {
            return false;
        }
    });



});



var isShowLock = false;

function lock($, layer) {
    if (isShowLock)
        return;
    //自定页
    layer.open({
        title: false,
        type: 1,
        closeBtn: 0,
        anim: 6,
        content: $('#lock-temp').html(),
        shade: [0.9, '#393D49'],
        area: ['350px', '220px'],
        success: function(layero, lockIndex) {
            isShowLock = true;
            //给显示用户名赋值
            layero.find('div#lockUserName').text('admin');
            layero.find('input[name=lockPwd]').on('focus', function() {
                    var $this = $(this);
                    if ($this.val() === '输入密码解锁..') {
                        $this.val('').attr('type', 'password');
                    }
                })
                .on('blur', function() {
                    var $this = $(this);
                    if ($this.val() === '' || $this.length === 0) {
                        $this.attr('type', 'text').val('输入密码解锁..');
                    }
                });
            //在此处可以写一个请求到服务端删除相关身份认证，因为考虑到如果浏览器被强制刷新的时候，身份验证还存在的情况
            /*
             $.post(url,params,callback,'json');
             */
            //绑定解锁按钮的点击事件
            layero.find('button#unlock').on('click', function() {
                var $lockBox = $('div#lock-box');

                var userName = $lockBox.find('div#lockUserName').text();
                var pwd = $lockBox.find('input[name=lockPwd]').val();
                if (pwd === '输入密码解锁..' || pwd.length === 0) {
                    layer.msg('请输入密码..', {
                        icon: 2,
                        time: 1000
                    });
                    return;
                }
                unlock(userName, pwd);
            });

            /**
             * 解锁操作方法
             * @param {String} 用户名
             * @param {String} 密码
             */
            var unlock = function(un, pwd) {
                //这里可以使用ajax方法解锁
                /*$.post('api/xx',{username:un,password:pwd},function(data){
                 	//验证成功
                	if(data.success){
                		//关闭锁屏层
                		layer.close(lockIndex);
                	}else{
                		layer.msg('密码输入错误..',{icon:2,time:1000});
                	}
                },'json');
                */
                isShowLock = false;
                //演示：默认输入密码都算成功
                //关闭锁屏层
                layer.close(lockIndex);
            };
        }
    });
}

//全屏
function fullScreen() {
    var docElm = document.documentElement;
    //W3C
    if (docElm.requestFullscreen) {
        docElm.requestFullscreen();
    }
    //FireFox
    else if (docElm.mozRequestFullScreen) {
        docElm.mozRequestFullScreen();
    }
    //Chrome等
    else if (docElm.webkitRequestFullScreen) {
        docElm.webkitRequestFullScreen();
    }
    //IE11
    else if (elem.msRequestFullscreen) {
        elem.msRequestFullscreen();
    }
    //layer.msg('按Esc即可退出全屏');
}